package com.cyys.common.utils.date;

import java.util.Date;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.core.util.ThreadSafeSimpleDateFormat;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
/**
 * 日期象转换为XML时的数据格式
 * @author LC
 *
 */
public class DateConverter implements Converter {
	private final ThreadSafeSimpleDateFormat defaultFormat;
	public DateConverter(){
		this("yyyy-MM-dd");
	}
	public DateConverter(String defaultFormat){
		this(defaultFormat,false);
	}
	public DateConverter(String defaultFormat,boolean lenient){
		this.defaultFormat = new ThreadSafeSimpleDateFormat("yyyy-MM-dd",null, 4,20,lenient);
	}
	public void marshal(Object source, HierarchicalStreamWriter writer,
			MarshallingContext context) {
		Date date = (Date)source;
		writer.setValue(defaultFormat.format(date));

	}

	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context) {
		return null;
	}

	public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
		return type.equals(Date.class);
	}

}
